<script setup>
import { ref } from "vue";
import {
  listRecord,
  updateRecord,
  addRecord,
  delRecord,
  getRecordInfo,
  listDept,
} from "@/api/hospital";
const { proxy } = getCurrentInstance();
import RecordViewerDialog from "@/views/hospital/components/RecordViewerDialog.vue";
import RecordEditDialog from "@/views/hospital/components/RecordEditDialog.vue";
const { sys_user_sex } = proxy.useDict("sys_user_sex");
import { parseTime } from "@/utils/ruoyi";
const list = ref([]);
const loading = ref(false);
const total = ref(0);
const deptList = ref([]);
const viewerDialogVisible = ref(false);
const viewerData = ref({});
const editDialogVisible = ref(false);
const editData = ref({});
const isEdit = ref(false);

// 查询参数
const data = reactive({
  queryParams: {
    pageNum: 1,
    patientName: undefined,
    dept_id: undefined,
    doctorName: undefined,
  },
});

const { queryParams } = toRefs(data);

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.patientName = undefined;
  queryParams.value.dept_id = undefined;
  queryParams.value.doctorName = undefined;
  handleQuery();
}

/** 列表查询 */
function getList() {
  loading.value = true;
  listRecord(queryParams.value).then((response) => {
    loading.value = false;
    list.value = response.data;
  });
}

/** 获取部门列表 */
function getDeptList() {
  listDept().then((res) => {
    deptList.value = res.data.slice(1); // 排除信息科
  });
}

/** 新增按钮操作 */
function handleAdd() {
  editData.value = {};
  isEdit.value = false;
  editDialogVisible.value = true;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const recordId = row.id;
  getRecordInfo(recordId).then((response) => {
    isEdit.value = true;
    editData.value = response.data;
    if (editData.value.first_visit) {
      editData.value.first_visit = parseTime(
        editData.value.first_visit,
        "{y}-{m}-{d}"
      );
    }
    editDialogVisible.value = true;
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const patientName = row.patientName;
  const recordId = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除患者姓名为"' + patientName + '"的该个档案？')
    .then(function () {
      return delRecord(recordId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 详细按钮操作 */
function handleView(row) {
  const recordId = row.id;
  getRecordInfo(recordId).then((response) => {
    viewerData.value = response.data;
    viewerDialogVisible.value = true;
  });
}

/** 提交按钮 */
function submitForm(result) {
  const formData = result.formData;
  if (formData.id != undefined) {
    updateRecord(formData).then((response) => {
      if (response.code == 200) proxy.$modal.msgSuccess("修改档案成功");
      else proxy.$modal.alertError(response.msg);
      open.value = false;
      getList();
    });
  } else {
    addRecord(formData).then((response) => {
      if (response.code == 200) proxy.$modal.msgSuccess("新增档案成功");
      else proxy.$modal.alertError(response.msg);
      open.value = false;
      getList();
    });
  }
}

getDeptList();
getList();
</script>
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="请输入姓名"
          clearable
          style="width: 150px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属科室" prop="deptName" style="width: 200px">
        <el-select
          filterable
          v-model="queryParams.dept_id"
          placeholder="请选择科室"
        >
          <el-option
            v-for="dict in deptList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属医生" prop="doctorName">
        <el-input
          v-model="queryParams.doctorName"
          placeholder="请输入所属医生"
          clearable
          style="width: 150px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="list">
      <el-table-column prop="id" label="档案号" align="center" width="100" />
      <el-table-column prop="patientName" label="姓名" align="center" />
      <el-table-column prop="genderStr" label="性别" align="center" />
      <el-table-column prop="age" label="年龄" align="center" />
      <el-table-column prop="outpatient_code" label="门诊号" align="center" />
      <el-table-column prop="inpatient_code" label="住院号" align="center" />
      <el-table-column prop="deptName" label="所属科室" align="center" />
      <el-table-column prop="doctorName" label="首诊医生" align="center" />
      <el-table-column label="操作" align="center" width="150">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="详细" placement="top">
            <el-button
              link
              type="primary"
              icon="View"
              @click="handleView(scope.row, scope.index)"
              >详细</el-button
            >
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <RecordEditDialog
      v-model="editDialogVisible"
      :edit-data="editData"
      :is-edit="isEdit"
      :dept-list="deptList"
      @confirm="submitForm"
    />
    <RecordViewerDialog
      v-model="viewerDialogVisible"
      :dialog-data="viewerData"
    />
  </div>
</template>
<style scoped></style>
